import sklearn.svm as SVM
import util.data_helper as data_helper
import baseline.dataset as dataset
import numpy as np
from sklearn.metrics import accuracy_score
import sklearn.metrics as metrics
from sklearn.externals import joblib  #也可以选择pickle等保存模型，请随意

bug_msg_all, _ = data_helper.get_msg_all()
vocabulary = data_helper.create_vocabulary()
developers_list = data_helper.create_developers_list()
time_windows = data_helper.split_dataset_by_eight_to_two(bug_msg_all)

print(len(time_windows[0]))

train_docs_list, train_label_list = dataset.get_index_of_features_and_labels(vocabulary, developers_list, bug_msg_all, time_windows[0])
eval_docs_list, eval_label_list = dataset.get_index_of_features_and_labels(vocabulary, developers_list, bug_msg_all, time_windows[1])
#
# print(np.array(train_docs_list).shape)
# print(np.array(train_label_list).shape)
#
# model = SVM.SVC(probability=True)
# model.fit(train_docs_list, train_label_list)
#
# joblib.dump(model, '../data/svm_gcc.model')
model = joblib.load('../data/svm_gcc.model')

# predict = model.predict(eval_docs_list)
# print(predict[0])
# for i in range(len(predict)):
# 	print(predict[i])
# print('{}\n'.format(predict[i]) for i in range(len(predict)))
predict_all = model.decision_function(eval_docs_list)
# predict_all = model._predict_proba(eval_docs_list)
print(len(predict_all[0]))
print(model.classes_)
top1_num = 0
top5_num = 0
for i in range(len(predict_all)):
	new_predict = np.argsort(predict_all[i])
	if eval_label_list[i] == model.classes_[new_predict[-1]]:
		top1_num += 1
	if model.classes_.index(eval_label_list[i]) in new_predict[-5:]:
		top5_num += 1
print(top1_num / len(predict_all))
print(top5_num / len(predict_all))
# print(predict_all[0])
#
# print(predict)
# print(accuracy_score(eval_label_list, predict))

